﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

using mbine.msgc.model;
using System.Threading.Tasks;
namespace mbine.msgc.controls
{
	/// <summary>
	/// Interaction logic for UCRoster.xaml
	/// </summary>
    public partial class UCRoster : UserControl, IObserveRoster
    {
        Point _lastMouseDown;
        CUser draggedItem;
        public event EventHandler EventChatStart;
        public List<IUser> SelectedUsers = new List<IUser>();

		public UCRoster()
		{
			this.InitializeComponent();
            tvRoster.MouseDoubleClick += new MouseButtonEventHandler(tvRoster_MouseDoubleClick);
            tvRoster.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(tvRoster_SelectedItemChanged);
        }

        void tvRoster_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (tvRoster.SelectedItem.GetType().Name == "CUser")
            {
                SelectedUsers.Clear();
                SelectedUsers.Add((IUser)tvRoster.SelectedItem);
                if (EventChatStart != null) EventChatStart(this, null);
            }
        }

        void tvRoster_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue != null)
            {
            }
        }

        public void ReloadRoster()
        {
            Dispatcher.Invoke(new ThreadStart(() =>
            {
                tvRoster.ItemsSource = (from p in CRoster.Instance.Parts where p.PartCode == "" select p).ToList();
            }));
        }
        
        #region private method
        void InitControl()
        {
            this.Margin = new Thickness(0);

            RowDefinition gridUserInfo = new RowDefinition();
            gridUserInfo.Height = new GridLength(100);
            RowDefinition gridRoster = new RowDefinition();

            LayoutRoot.RowDefinitions.Add(gridUserInfo);
            LayoutRoot.RowDefinitions.Add(gridRoster);
        }
        #endregion

        public void UpdateRoster()
        {
            ReloadRoster();
        }

        #region Item Drag & Drop
        private void tvRoster_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point currentPosition = e.GetPosition(tvRoster);

                if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > 10.0) ||
                    (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > 10.0))
                {
                    draggedItem = (CUser)tvRoster.SelectedItem;
                    if (draggedItem != null)
                    {
                        DragDrop.DoDragDrop(tvRoster, tvRoster.SelectedValue, DragDropEffects.Move);
                    }
                }
            }
        }
        private void tvRoster_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _lastMouseDown = e.GetPosition(tvRoster);
        }
        #endregion

        private void tvRoster_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(Task)))
            {
                e.Effects = DragDropEffects.Move;
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }
    }
}